$(function () {
    var movie_id = $("#movie_id").attr("data-val");
    load_lines(movie_id);
    load_movies(movie_id);
    load_imgs(movie_id);
    action();
    setTimeout(movie_click(movie_id),2000);
    setTimeout(get_comment(movie_id),2000);
    user_rating(movie_id);
    active();
});

//介绍、名句、图集跳转
function active() {
    $('.tab-desc-click').click(function () {
        $('.tab-title').removeClass('active');
        $('.tab-content').removeClass('active');
        $('.tab-desc-click').addClass('active');
        $('.tab-desc').addClass('active');
    });
    $('.tab-celebrity-click').click(function () {
        $('.tab-title').removeClass('active');
        $('.tab-content').removeClass('active');
        $('.tab-celebrity-click').addClass('active');
        $('.tab-celebrity').addClass('active');
    });
    $('.tab-img-click').click(function () {
        $('.tab-title').removeClass('active');
        $('.tab-content').removeClass('active');
        $('.tab-img-click').addClass('active');
        $('.tab-img').addClass('active');
    });
}

//用户评分
function user_rating(movie_id) {
    var user_id = $('#user_id').text();
    $.get('/user/user_rating/',{'movie_id':movie_id, 'user_id':user_id, 'type': '0'},function (data) {
        var jsonObj = JSON.parse(data);
        var rating = jsonObj['rating'];
        if(rating != null){
            $('#rating').text(rating['rating']);
        }
    });
    $('#rating_click').click(function () {
        if(user_id != -1){
            rating = $('#rating').text();
            $.get('/user/user_rating/',{'movie_id':movie_id, 'user_id':user_id, 'type': '1','rating':rating},function (data) {
            var jsonObj = JSON.parse(data);
            var info = jsonObj['info'];
            $('#comment-form-container').css('display','none');
            if (info == 0) alert('评分失败');
            else alert('评分成功')
    });
        }
        else alert('请先登录')
    });

}

//根据用户id和电影id判断用户是否对此电影点过赞或是收藏或是评分,用户点赞，收藏动作
function movie_click(movie_id) {
    var user_id = $('#user_id').text();
    $.get('/user/is_active/',{'media_id':movie_id, 'user_id':user_id, 'type': '0'},function (data) {
        var jsonObj = JSON.parse(data);
        var parise_num = jsonObj['parise'];
        var star_num = jsonObj['star'];
        if(parise_num != null){
            $('.wish').addClass('active');
        }
        if(star_num != null){
            $('.score-btn').addClass('active');
        }
    });
    var parise = $('.wish');
    var star = $('.score-btn');
    parise.click(function () {
        if (user_id != -1) {
            var i = $(this);
            console.log(movie_id);
            $.get('/user/parise_star/', {'media_id': movie_id, 'user_id': user_id,'active':0, 'type': '0'}, function (data) {
                var jsonObj = JSON.parse(data);
                var type = jsonObj['type'];
                if (type == '1') i.addClass('active');
                else i.removeClass('active');
            });
        }
        else {
            alert('请先登录')
        }
    });
    star.click(function () {
       if (user_id != -1) {
            var i = $(this);
            $.get('/user/parise_star/', {'media_id': movie_id, 'user_id': user_id, 'active':1, 'type': '0'}, function (data) {
                var jsonObj = JSON.parse(data);
                var type = jsonObj['type'];
                if (type == '1') i.addClass('active');
                else i.removeClass('active');
            });
        }
        else {
            alert('请先登录')
        }
    });

}

function action() {
    $('.set_score').click(function () {
        $('#comment-form-container').css('display','block');
        $('#comment-form-container').css('opacity','1');
    })
    $('.close').click(function () {
        $('#comment-form-container').css('display','none');
        $('#comment-form-container').css('opacity','0');

})};

function load_imgs(movie_id) {
    $.get('/picture/movie_imgs/',{'movie_id':movie_id}, function (data,status) {
        var jsonObj = JSON.parse(data);
        var imgs = jsonObj['info'];
        $('#s1_img').html('');
        $('#all_img').html('');
        for(var i=0; i<imgs.length; i++){
            var temp = imgs[i];
            if (i<5){
                htmltr = ' <div class="img1" img_id="'+ temp['id'] +'"><a href="/picture/single/?pid='+ temp['id'] +'"><img class="default-img" alt="" ' +
                'src="'+ temp['url'] +'"></a></div>\n';
            $('#s1_img').append(htmltr);
            }
            htmltr = '<li>\n' +
                '          <a href="/picture/single/?pid='+ temp['id'] +'">      <img class="default-img" data-act="movie-img-click" alt="" img_id="'+ temp['id'] +'" ' +
                'src="'+ temp['url'] +'"></a>\n' +
                '              </li>'
            $('#all_img').append(htmltr);
        }
    })
}

function load_movies(movie_id) {
    $.get('/movie/other_movies/',{'movie_id':movie_id}, function (data,status) {
        var jsonObj = JSON.parse(data);
        if(jsonObj.status == 200) {
            var movie = jsonObj['info'];
            $('#other_movies').html('');
            for(var i=0; i<movie.length; i++){
                var temp = movie[i];
                var score = temp['score']+'.';
                var str = score.toString().split('.');
                htmltr = '<dd>\n' +
                    '    <div class="movie-item">\n' +
                    '      <a href="/movie/single/?movie_id='+ temp['id'] +'"  data-act="movie-click1" data-val="'+ temp['id'] +'">\n' +
                    '      <div class="movie-poster">\n' +
                    '        <img class="poster-default" src="'+ temp['url'] +'">\n' +
                    '        <img src="'+ temp['url'] +'">\n' +
                    '      </div>\n' +
                    '      </a>\n' +
                    '      <div class="movie-ver"></div>\n' +
                    '    </div>\n' +
                    '    <div class="channel-detail movie-item-title" title="'+ temp['movie_name'] +'">\n' +
                    '      <a href="/movie/single/?movie_id='+ temp['id'] +'"  data-act="movies-click" data-val="'+ temp['id'] +'">'+ temp['movie_name'] +'</a>\n' +
                    '    </div>\n' +
                    '<div class="channel-detail channel-detail-orange"><i class="integer">'+ str[0] +'.</i><i class="fraction">'+ str[1] +'</i></div>\n' +
                    '  </dd>';
                $('#other_movies').append(htmltr);
            }
        } else {
            // 错误码处理
            console.log('请求错误！');
        }

    })
}

function load_lines(movie_id) {
    $.get('/movie/movie_lines/',{'movie_id':movie_id},function (data,status) {
        var jsonObj = JSON.parse(data);
        var movie = jsonObj['info'];
        $('#lines_num').text('('+movie.length+')');
        $("#movie_lines").html('');
        if(movie.length>0){
            for(var i=0; i<movie.length; i++){
                var temp = movie[i];
                htmltr = '<li  class="name movie_line">\n' +
                    '      <p>'+ temp['line'] +'</p>\n' +
                    '    </li>';
                $("#movie_lines").append(htmltr);
        }
        }
        else{
            htmltr = '<li  class="name movie_line">\n' +
                    '     暂无 \n' +
                    '    </li>';
            $("#movie_lines").append(htmltr);
        }

    })
}